package edu.gyc.hishiro.controller;


import edu.gyc.hishiro.model.UserInfo;
import edu.gyc.hishiro.service.UserInfoService;
import edu.gyc.hishiro.shiro.utils.PasswordEncrypter;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ls
 * @since 2020-06-24
 */
@Controller

public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/useradd")
    @RequiresPermissions("userinfo:add")//权限管理;
    public String userInfoAdd(UserInfo userInfo, Model model) {



        return "userInfoAdd";
    }

    @RequestMapping("/doreg")
    public String doreg(UserInfo userInfo,Model model) {
        String msg="";
        if (!userInfo.getPassword().equals(userInfo.getPassword2()) ) {
            msg = "密码不相同！";
        }else {
            UserInfo tmpuser = userInfoService.lambdaQuery().eq(UserInfo::getUsername, userInfo.getUsername()).one();
            msg = "恭喜" + userInfo.getUsername() + "注册成功";
            if (tmpuser == null) {
                PasswordEncrypter passwordEncrypter = new PasswordEncrypter<UserInfo>();
                passwordEncrypter.encryptPassword(userInfo);
                userInfoService.save(userInfo);
            } else {
                msg = "用户名重复！";
            }
            model.addAttribute("msg", msg);
        }
        return "reg";
    }
}

